package com.gin.kotlin.no_gay_alliance.adapter.emoji

import android.content.Context
import com.gin.kotlin.no_gay_alliance.activity.emoji.EmojiGroupPreviewActivity
import com.gin.kotlin.no_gay_alliance.base.AbstractBindingRecyclerViewAdapter
import com.gin.kotlin.no_gay_alliance.base.MyHandler
import com.gin.kotlin.no_gay_alliance.databinding.ItemEmojiGroupBinding
import com.gin.kotlin.no_gay_alliance.model.emoji.GenericEmojiGroup
import com.gin.kotlin.no_gay_alliance.store.GlobalProperty

class GenericEmojiGroupAdapter(context: Context, handler: MyHandler, data: MutableList<GenericEmojiGroup> = mutableListOf()) :
    AbstractBindingRecyclerViewAdapter<ItemEmojiGroupBinding, GenericEmojiGroup>(context, handler, false, data) {
    override fun onBindViewHolder(holder: ViewHolder<ItemEmojiGroupBinding>, position: Int, b: ItemEmojiGroupBinding, dataModel: GenericEmojiGroup) {
        // 唯一名称
        b.root.text = dataModel.name
        b.root.tag = dataModel
        b.root.isChecked = !GlobalProperty.getInstance().hiddenGenericEmojiGroup.contains(dataModel.getUniqueName())

        //勾选修改配置
        b.root.setOnCheckedChangeListener { buttonView, isChecked ->
            (buttonView.tag as? GenericEmojiGroup)?.also { group ->
                val name = group.getUniqueName()
                val groupNames = GlobalProperty.getInstance().hiddenGenericEmojiGroup
                if (isChecked) {
                    groupNames.remove(name)
                } else {
                    groupNames.add(name)
                }
                GlobalProperty.getInstance().hiddenGenericEmojiGroup = groupNames
            }
        }
        // 长按打开预览
        b.root.setOnLongClickListener {
            (it.tag as? GenericEmojiGroup)?.also { group -> EmojiGroupPreviewActivity.startActivity(context, group) }
            true
        }
    }
}